package com.jindianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jindianjichi.basic.entity.SubjectLabel;
import com.jindianjichi.subject.application.convert.SubjectLabelDtoConverter;
import com.jindianjichi.subject.application.entity.SubjectLabelDto;
import com.jindianjichi.subject.common.entity.Result;
import com.jindianjichi.subject.domain.convert.SubjectLabelConvert;
import com.jindianjichi.subject.domain.entity.SubjectLabelBo;
import com.jindianjichi.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@Slf4j
@RequestMapping("/subject/label")
public class SubjectLabelController {
    @Resource
    SubjectLabelDomainService subjectLabelDomainService;
    @PostMapping("/add")
    public Result addLabel(@RequestBody SubjectLabelDto subjectLabelDto){
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectLabelDto));
            }
            Preconditions.checkNotNull(subjectLabelDto.getLabelName(),"标签名称不能为空");
            Preconditions.checkNotNull(subjectLabelDto.getCategoryId(),"分类Id不能为空");
            //将Dto转化为Bo
            SubjectLabelBo subjectLabelBo = SubjectLabelDtoConverter.instance.convertToBoLabel(subjectLabelDto);
            log.info("SubjectCategoryController.addLabel.Bo:{}", JSON.toJSONString(subjectLabelBo));
            subjectLabelDomainService.insertLabel(subjectLabelBo);
            return Result.ok("新增标签成功");
        } catch (Exception e) {
            log.error("SubjectLabelController.addLabel.error:{}", e.getMessage(), e);
            return Result.fail("删除失败");
        }

    }
    @PostMapping("/delete")
    public Result<Boolean> deletedLabel(@RequestBody SubjectLabelDto subjectLabelDto){
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.deletedLabel.dto:{}", JSON.toJSONString(subjectLabelDto));
            }
            //判断参数
            Preconditions.checkNotNull(subjectLabelDto.getId(),"要删除的标签的id不能为空");
            //将dto转化为Bo
            SubjectLabelBo subjectLabelBo = SubjectLabelDtoConverter.instance.convertToBoLabel(subjectLabelDto);
            //调用Domain曾接口
            subjectLabelDomainService.deletedLabelById(subjectLabelBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectLabelController.deletedLabel.error:{}", e.getMessage(), e);
            return Result.fail("删除标签失败");
        }

    }


    @PostMapping("/update")
    public Result<Boolean> updateLabel(@RequestBody SubjectLabelDto subjectLabelDto){
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.deletedLabel.dto:{}", JSON.toJSONString(subjectLabelDto));
            }
            //判断参数
            Preconditions.checkNotNull(subjectLabelDto.getId(),"要更新的标签的id不能为空");
            //将dto转化为Bo
            SubjectLabelBo subjectLabelBo = SubjectLabelDtoConverter.instance.convertToBoLabel(subjectLabelDto);
            //调用Domain曾接口
            subjectLabelDomainService.updateLabelById(subjectLabelBo);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectLabelController.deletedLabel.error:{}", e.getMessage(), e);
            return Result.fail("更新标签失败");
        }

    }
    @PostMapping("/queryLabelByCategoryId")
    public Result<List<SubjectLabelDto>> queryLabelByCategoryId(@RequestBody SubjectLabelDto subjectLabelDto){
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.queryLabelByCategoryId.dto:{}", JSON.toJSONString(subjectLabelDto));
            }
            //判断参数
            Preconditions.checkNotNull(subjectLabelDto.getCategoryId(),"分类的id分能为空");
            //将dto转化为Bo
            SubjectLabelBo subjectLabelBo = SubjectLabelDtoConverter.instance.convertToBoLabel(subjectLabelDto);
            //调用Domain曾接口
            List<SubjectLabelBo> subjectLabelBos = subjectLabelDomainService.selectSubjectLabelByCondition(subjectLabelBo);
            List<SubjectLabelDto> subjectLabelDtos = SubjectLabelDtoConverter.instance.convertToDtoLabel(subjectLabelBos);
            return Result.ok(subjectLabelDtos);
        } catch (Exception e) {
            log.error("SubjectLabelController.deletedLabel.error:{}", e.getMessage(), e);
            return Result.fail("更新标签失败");
        }

    }
}
